package com.glodon.paas.monitor.web.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.glodon.paas.consts.StringConst;
import com.glodon.paas.monitor.server.biz.domain.MonitorApplicationEntity;
import com.glodon.paas.monitor.server.biz.domain.MonitorItemEntity;
import com.glodon.paas.monitor.server.biz.domain.MonitorPluginEntity;
import com.glodon.paas.monitor.server.biz.service.MonitorPluginService;
import com.glodon.paas.monitor.server.constants.MonitorStatusEnum;
import com.glodon.paas.monitor.server.dto.MonitorItemDTO;
import com.glodon.paas.monitor.server.dto.MonitorResultMsg;
import com.glodon.paas.monitor.server.exposed.facade.MonitorAppUserFacadeService;
import com.glodon.paas.monitor.server.exposed.facade.MonitorItemAlarmSendFacadeService;
import com.glodon.paas.monitor.server.util.DateFormatUtil;
import com.glodon.paas.monitor.web.constants.MonitorWebPageConstants;

@Controller
public class MonitorItemController {

	@Autowired
	private MonitorItemAlarmSendFacadeService	monitorItemAlarmSendFacadeService;

	@Autowired
	private MonitorAppUserFacadeService			monitorAppUserFacadeService;

	@Autowired
	private MonitorPluginService				monitorPluginService;

	@RequestMapping(value = "itemMain", method = RequestMethod.GET)
	public ModelAndView indexMonitorItem(@RequestParam(value = "appId", required = false) Long appId,
			@RequestParam(value = "pluginId", required = false) Long pluginId,
			@RequestParam(value = "itemName", required = false) String itemName,
			@RequestParam(value = "itemStatus", required = false) String itemStatus, ModelMap model,
			HttpServletRequest request) {
		MonitorApplicationEntity entity = new MonitorApplicationEntity();
		MonitorResultMsg<ArrayList<MonitorApplicationEntity>> entityrReslut = monitorAppUserFacadeService
				.queryMonitorApplication(entity);
		if (null != entityrReslut) {
			if (entityrReslut.isSuccess()) {
				ArrayList<MonitorApplicationEntity> entityList = entityrReslut.getResultObject();
				model.addAttribute(MonitorWebPageConstants.APP_LIST, entityList);
			}
		}

		List<MonitorPluginEntity> pluginList = monitorPluginService.listAllPlugin();
		if (null != pluginList && !pluginList.isEmpty()) {
			model.addAttribute(MonitorWebPageConstants.PLUGIN_LIST, pluginList);
		}

		boolean modelFlag = false;
		ModelMap doItemModel = (ModelMap) request.getSession().getAttribute("doItemModel");
		if (null != doItemModel && !doItemModel.isEmpty()) {
			modelFlag = true;
			itemName = (String) doItemModel.get(StringConst.ITEM);
		}

		MonitorItemDTO itemDto = new MonitorItemDTO();
		if (null != appId) {
			itemDto.setAppId(appId);
		}

		if (null != pluginId) {
			itemDto.setPluginId(pluginId);
		}

		if (StringUtils.isNotBlank(itemName)) {
			itemDto.setItemName(itemName);
		}

		if (StringUtils.isNotBlank(itemStatus)) {
			itemDto.setItemStatus(itemStatus);
		}

		MonitorResultMsg<ArrayList<MonitorItemDTO>> itemResultMsg = monitorItemAlarmSendFacadeService
				.queryMonitorItem(itemDto);

		if (null != itemResultMsg) {
			if (itemResultMsg.isSuccess()) {
				ArrayList<MonitorItemDTO> itemList = (ArrayList<MonitorItemDTO>) itemResultMsg.getResultObject();
				model.addAttribute(StringConst.ITEMS, itemList);
				model.addAttribute(StringConst.RESULT, StringConst.SUCCESS);
			} else {
				model.addAttribute(StringConst.RESULT, StringConst.FAIL);
				model.addAttribute(StringConst.RETURN_MSG, itemResultMsg.getResultMsg());
			}
		} else {
			model.addAttribute(StringConst.RESULT, StringConst.FAIL);
			model.addAttribute(StringConst.RETURN_MSG, MonitorWebPageConstants.QUERY_RESULT_EMPTY);
		}

		if (modelFlag) {
			model.addAttribute(StringConst.RETURN_MSG, doItemModel.get(StringConst.RETURN_MSG));
			model.addAttribute(StringConst.RESULT, doItemModel.get(StringConst.RESULT));
			request.getSession().removeAttribute("doItemModel");
		}
		return new ModelAndView("itemMain", model);
	}

	@RequestMapping(value = "itemAction", method = RequestMethod.GET)
	public ModelAndView indexItemAction(@RequestParam(value = "id", required = false) Long id, ModelMap model) {
		MonitorApplicationEntity entity = new MonitorApplicationEntity();
		MonitorResultMsg<ArrayList<MonitorApplicationEntity>> entityrReslut = monitorAppUserFacadeService
				.queryMonitorApplication(entity);
		if (null != entityrReslut) {
			if (entityrReslut.isSuccess()) {
				ArrayList<MonitorApplicationEntity> entityList = entityrReslut.getResultObject();
				model.addAttribute(MonitorWebPageConstants.APP_LIST, entityList);
			}
		}

		List<MonitorPluginEntity> pluginList = monitorPluginService.listAllPlugin();
		if (null != pluginList && !pluginList.isEmpty()) {
			model.addAttribute(MonitorWebPageConstants.PLUGIN_LIST, pluginList);
		}

		MonitorResultMsg<MonitorItemEntity> resultMsg = monitorItemAlarmSendFacadeService.findMonitorItemById(id);
		if (null == resultMsg || !resultMsg.isSuccess()) {
			model.addAttribute(StringConst.RESULT, StringConst.FAIL);
			model.addAttribute(StringConst.RETURN_MSG, MonitorWebPageConstants.INVALID_PARAMETER);
			return new ModelAndView("itemAction", model);
		}
		MonitorItemEntity itemEntity = (MonitorItemEntity) resultMsg.getResultObject();
		model.addAttribute("itemEntity", itemEntity);
		model.addAttribute("itemId", id);
		return new ModelAndView("itemAction", model);
	}

	@RequestMapping(value = "doItemAction", method = RequestMethod.POST)
	public ModelAndView doItemAction(@RequestParam(value = "itemId", required = false) Long itemId,
			@RequestParam(value = "appId", required = false) Long appId,
			@RequestParam(value = "pluginId", required = false) Long pluginId,
			@RequestParam(value = "required", required = false) String required,
			@RequestParam(value = "itemFiled", required = false) String itemFiled,
			@RequestParam(value = "number", required = false) Integer number,
			@RequestParam(value = "effectiveInterval", required = false) Integer effectiveInterval, ModelMap model,
			HttpServletRequest request) {
		if (null != itemId) {
			MonitorResultMsg<MonitorItemEntity> resultMsg = monitorItemAlarmSendFacadeService
					.findMonitorItemById(itemId);
			if (null != resultMsg && resultMsg.isSuccess()) {
				MonitorItemEntity itemEntity = (MonitorItemEntity) resultMsg.getResultObject();
				itemEntity.setUpdateTime(new Date());
				itemEntity.setAppId(appId);
				itemEntity.setPluginId(pluginId);
				itemEntity.setItemName(required);
				itemEntity.setItemFiled(itemFiled);
				itemEntity.setAssignInterval(number);
				itemEntity.setEffectiveInterval(effectiveInterval);
				if (null != effectiveInterval) {
					itemEntity.setEffectiveTime(DateFormatUtil.parse(DateFormatUtil
							.getIntervalMinute(effectiveInterval)));
				}
				MonitorResultMsg<MonitorItemEntity> itemResultMsg = monitorItemAlarmSendFacadeService
						.updateMonitorItem(itemEntity);
				if (null != itemResultMsg) {
					if (itemResultMsg.isSuccess()) {
						model.addAttribute(StringConst.RESULT, StringConst.SUCCESS);
						model.addAttribute(StringConst.RETURN_MSG, MonitorWebPageConstants.UPDATE_SUCCESS);
					} else {
						model.addAttribute(StringConst.RESULT, StringConst.FAIL);
						model.addAttribute(StringConst.RETURN_MSG, itemResultMsg.getResultMsg());
					}
				} else {
					model.addAttribute(StringConst.RESULT, StringConst.FAIL);
					model.addAttribute(StringConst.RETURN_MSG, MonitorWebPageConstants.UPDATE_FAIL);
				}
			}
		} else {
			MonitorItemEntity itemEntity = new MonitorItemEntity();
			itemEntity.setCreateTime(new Date());
			itemEntity.setAppId(appId);
			itemEntity.setPluginId(pluginId);
			itemEntity.setItemName(required);
			itemEntity.setItemFiled(itemFiled);
			itemEntity.setAssignInterval(number);
			itemEntity.setItemStatus(MonitorStatusEnum.OFF.getCode());
			itemEntity.setEffectiveInterval(effectiveInterval);
			if (null != effectiveInterval) {
				itemEntity.setEffectiveTime(DateFormatUtil.parse(DateFormatUtil.getIntervalMinute(effectiveInterval)));
			}
			MonitorResultMsg<MonitorItemEntity> itemResultMsg = monitorItemAlarmSendFacadeService
					.saveMonitorItem(itemEntity);
			if (null != itemResultMsg) {
				if (itemResultMsg.isSuccess()) {
					model.addAttribute(StringConst.RESULT, StringConst.SUCCESS);
					model.addAttribute(StringConst.RETURN_MSG, MonitorWebPageConstants.CREATE_SUCCESS);
				} else {
					model.addAttribute(StringConst.RESULT, StringConst.FAIL);
					model.addAttribute(StringConst.RETURN_MSG, itemResultMsg.getResultMsg());
				}
			} else {
				model.addAttribute(StringConst.RESULT, StringConst.FAIL);
				model.addAttribute(StringConst.RETURN_MSG, MonitorWebPageConstants.CREATE_FAIL);
			}
		}
		model.addAttribute(StringConst.ITEM, required);
		request.getSession().setAttribute("doItemModel", model);
		return new ModelAndView(new RedirectView("itemMain"));
	}

	@RequestMapping(value = "deleteItem", method = RequestMethod.GET)
	public ModelAndView doItemDelete(@RequestParam("id") Long id, ModelMap model, HttpServletRequest request) {
		if (null == id) {
			model.addAttribute(StringConst.RESULT, StringConst.FAIL);
			model.addAttribute(StringConst.RETURN_MSG, MonitorWebPageConstants.INVALID_PARAMETER);
			request.getSession().setAttribute("doItemModel", model);
			return new ModelAndView(new RedirectView("itemMain"));
		}
		MonitorResultMsg<Boolean> resultMsg = monitorItemAlarmSendFacadeService.deleteMonitorItem(id);
		if (null != resultMsg) {
			if (resultMsg.isSuccess()) {
				model.addAttribute(StringConst.RESULT, StringConst.SUCCESS);
				model.addAttribute(StringConst.RETURN_MSG, MonitorWebPageConstants.DELETE_SUCCESS);
			} else {
				model.addAttribute(StringConst.RESULT, StringConst.FAIL);
				model.addAttribute(StringConst.RETURN_MSG, resultMsg.getResultMsg());
			}
		} else {
			model.addAttribute(StringConst.RESULT, StringConst.FAIL);
			model.addAttribute(StringConst.RETURN_MSG, MonitorWebPageConstants.DELETE_FAIL);
		}
		request.getSession().setAttribute("doItemModel", model);
		return new ModelAndView(new RedirectView("itemMain"));
	}

	@RequestMapping(value = "changeStatusItem", method = RequestMethod.GET)
	public ModelAndView doItemChangeStatus(@RequestParam("id") Long id, @RequestParam("itemStatus") String itemStatus,
			ModelMap model, HttpServletRequest request) {
		if (null == id || StringUtils.isBlank(itemStatus)) {
			model.addAttribute(StringConst.RESULT, StringConst.FAIL);
			model.addAttribute(StringConst.RETURN_MSG, MonitorWebPageConstants.INVALID_PARAMETER);
			request.getSession().setAttribute("doItemModel", model);
			return new ModelAndView(new RedirectView("itemMain"));
		}

		MonitorResultMsg<MonitorItemEntity> resultMsg = monitorItemAlarmSendFacadeService.changeMonitorItemStatus(id,
				itemStatus);
		if (null != resultMsg) {
			if (resultMsg.isSuccess()) {
				MonitorItemEntity entity = (MonitorItemEntity) resultMsg.getResultObject();
				model.addAttribute(StringConst.ITEM, entity.getItemName());
				model.addAttribute(StringConst.RESULT, StringConst.SUCCESS);
				model.addAttribute(StringConst.RETURN_MSG, MonitorWebPageConstants.CHANGE_SUCCESS);
			} else {
				model.addAttribute(StringConst.RESULT, StringConst.FAIL);
				model.addAttribute(StringConst.RETURN_MSG, resultMsg.getResultMsg());
			}
		} else {
			model.addAttribute(StringConst.RESULT, StringConst.FAIL);
			model.addAttribute(StringConst.RETURN_MSG, MonitorWebPageConstants.CHANGE_FAIL);
		}
		request.getSession().setAttribute("doItemModel", model);
		return new ModelAndView(new RedirectView("itemMain"));
	}
}
